package com.kynnnn.cloud.spring_cache_redis.controller;

import com.kynnnn.cloud.spring_cache_redis.constant.UserConstant;
import com.kynnnn.cloud.spring_cache_redis.entity.SysUser;
import com.kynnnn.cloud.spring_cache_redis.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

/**
 * @author Zhudiwei
 * @date 2021/09/16
 **/
@RestController
@RequestMapping("/user")
@Slf4j
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @PostMapping("/save")
    public Object save(@RequestBody SysUser user) {
        return userService.save(user);
    }

    @PostMapping("/update")
    public Object update(@RequestBody SysUser user) {
        return userService.updateUser(user);
    }


    @GetMapping("/list")
    @Cacheable(value = UserConstant.CACHE_USERS)
    public Object list() {
        log.debug("进入list方法");
        return userService.list();
    }

    @GetMapping("/get")
    @Cacheable(value = UserConstant.CACHE_USER_ID, key = "#id")
    public Object getById(@RequestParam("id") String id) {
        return userService.getById(id);
    }

}
